CREATE TRIGGER enrollmentLimit ON enroll
AFTER INSERT 
AS
BEGIN
DECLARE @enrollCount INTEGER
DECLARE @limit INTEGER
DECLARE @insertedID INTEGER  

SELECT @insertedID = (SELECT section_id FROM inserted)
SELECT @enrollCount = (SELECT count(*) FROM enroll WHERE enroll.section_id = @insertedID)
SELECT @limit = (SELECT limit FROM section WHERE section.section_id = @insertedID)
IF (@enrollCount > @limit)
	BEGIN
		DECLARE @newStudent VARCHAR(50)
		SELECT @newStudent = (SELECT student_id FROM inserted)
		DELETE FROM enroll WHERE section_id = @insertedID AND student_id = @newStudent
		RAISERROR ('Sorry, the section is already full.',12, 1)
	END
END
